<?php

namespace App\Domain\ChannelBusiness\Controllers;

use App\Domain\ChannelBusiness\Dao\ChannelBusinessDao;
use App\Domain\ChannelBusiness\Requests\updateChannelPost;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Domain\ChannelBusiness\Services\ChannelBusinessService;
use App\Domain\ChannelBusiness\Requests\createChannelPost;
use App\Domain\ChannelBusiness\Models\ChannelBusinessModel;

class ChannelBusinessController extends Controller
{
    //渠道商列表
    public function list(Request $request, ChannelBusinessService $channelBusinessService)
    {
        $this->validate($request, [
            'name' => 'string|nullable',
            'type' => 'string|nullable',
            'status' => 'string|nullable',
            'phone' => 'string|nullable',
            'contacts' => 'string|nullable',
            'province' => 'string|nullable',
            'city' => 'string|nullable',
            'area' => 'string|nullable',
            'term_start' => 'string|nullable',
            'term_end' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'name' => $request['name'],
            'type' => $request['type'],
            'status' => $request['status'],
            'phone' => $request['phone'],
            'contacts' => $request['contacts'],
            'province' => $request['province'],
            'city' => $request['city'],
            'area' => $request['area'],
            'term_start' => $request['term_start'],
            'term_end' => $request['term_end'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];
        $channelPaginator = $channelBusinessService->list($param);
        $items = array_map(function ($val) {
            if($val['status']==1){
                $val['status_text'] = '合作中';
            }else{
                 $val['status_text'] = '合作终止';
            }
            $val['region'] = $val['province'].'-'.$val['city'];
            return $val;
        }, $channelPaginator->items());
        $channelPaginator = new LengthAwarePaginator($items, $channelPaginator->total(), $channelPaginator->perPage(), $channelPaginator->currentPage());
        //格式化
        $keyMap = [
            'id' => 'id',
            'name' => 'name',
            'type' => 'type',
            'phone' => 'phone',
            'region' => 'region',
            'contacts' => 'contacts',
            'address' => 'address',
            'term_start' => 'term_start',
            'term_end' => 'term_end',
            'activation' => 'activation',
            'province' => 'province',
            'city' => 'city',
            'area' => 'area',
            'status' => 'status',
            'is_open' => 'is_open',
            'is_interface' => 'is_interface',
            'status_text' => 'status_text',
            'account' => 'account',
            'salesman' => 'salesman',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($channelPaginator, $keyMap));
    }


    //创建渠道商
    public function createChannel(createChannelPost $request, ChannelBusinessService $channelBusinessService)
    {
        $param = $request->validated();
        $res = $channelBusinessService->create($param);
        return $this->success($res);
    }

    //更新渠道商
    public function updateChannel(updateChannelPost $request, ChannelBusinessService $channelBusinessService)
    {
        $param = $request->validated();

        $res = $channelBusinessService->updateChannel($param);

        return $this->success($res);
    }

    //删除渠道商
    public function del(Request $request, ChannelBusinessService $channelBusinessService)
    {
        $res = $channelBusinessService->del($request['id']);
        return $this->success($res);
    }

    //设备接口
    public function equipmentInterface(Request $request)
    {
        $id = $request['id'];
        $data = (new ChannelBusinessModel())::where('id', $id)->first(['name', 'type', 'phone', 'contacts', 'address', 'province', 'city', 'area', 'data_upload_interface','is_interface']);
        return $this->success($data);
    }

    //设备接口修改
    public function updateEquipment(Request $request, ChannelBusinessService $channelBusinessService)
    {
        $res = $channelBusinessService->updateEquipment($request);

        return $this->success($res);
    }

    //密码重置
    public function restsecret(Request $request, ChannelBusinessService $channelBusinessService)
    {
        $data = $channelBusinessService->ResetPassword($request['account']);
        return $this->success($data);
    }

    //所有渠道商
    public function allChannel(ChannelBusinessService $channelBusinessService){
        $data= $channelBusinessService->findAll();
        return $this->success($data);
    }

}
